/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function wishList(){
    var div=document.getElementById("wishList");
    if(div.style.display=="none"){
        div.style.display="block";
    }else{
        div.style.display="none";
    }
}
function dragDrop(){
    
    var hijos=$('tareasDia').getElementsByTagName('div');
    var i=0;
    //alert(hijos.length);
    while(i<hijos.length){
                new Draggable(hijos[i].id,{
                revert:true,
                ghosting:true

            });
            i++;
           //alert("entra en hijos");
    }

    Droppables.add('wishList',{
            accept:'tarea',
            hoverclass:'onDrag',
            onDrop:function(element){
            var div=new Element('div',{'id':element.id});
            var lista_p=element.getElementsByTagName('p');
            i=0;
            alert(lista_p.length);
            while(i<lista_p.length){
                var texto=lista_p[i].firstChild.nodeValue;
                var contenido=document.createTextNode(texto);
                var p=new Element('p');
                p.appendChild(contenido);
                div.appendChild(p);
                i++;
            }
            $('wishList').insert(div);
            element.remove();
    }});
 }
 function createXMLHttpRequest() {
    var xmlHttp;
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
    return xmlHttp;
}
function login(){

     var xhr;
     var usuario = document.getElementById("usuario");
     var contrasena = document.getElementById("contrasena");
     xhr=createXMLHttpRequest();
     xhr.open("GET","controller?usuario="+usuario.value+"&contrasena="+contrasena.value,true);
     xhr.onreadystatechange=function(){

         mostrar_XML(xhr);
     };
     xhr.send(null);
}
function  mostrar_XML(xhr){
    var XML;
    if(xhr.readyState==4){
        if(xhr.status==200){
          XML=xhr.responseText;
          document.getElementById("usuario").value="";
      }
    }
}

function ingresoTarea(){

     var xhr=createXMLHttpRequest();
     xhr.open("GET","Tareas".value,true);
     xhr.onreadystatechange=function(){

         mostrar_XML(xhr);
     };
     xhr.send(null);
}
function consultarTareasTodas(){
     var xhr;
     xhr=createXMLHttpRequest();
     xhr.open("GET","../ManejadorTareas?action=consultarTodas",true);
     xhr.onreadystatechange=function(){
        
         mostrarTareasTodas(xhr);
     };
     xhr.send(null);
}
function mostrarTareasTodas(){

				//var map=new GMap2(document.getElementById("mapa"));
			 //map.setMapType(G_SATELLITE_3D_MAP);
            //  map.addControl(new GLargeMapControl());
             // map.addControl(new GMapTypeControl());
              //map.addControl(new GOverviewMapControl());
              //map.addControl(new GScaleControl());
			 // map.setCenter(new GLatLng(-2.186392, -79.922576), 13);

			//this.formatImg = function(elCell, oRecord, oColumn, sData) {
			//		elCell.innerHTML = "<img src=" + oRecord.getData("foto") + " />";
			//	};//para colocar la imagen

				var myColumnDefs = [
                                        
					{key:"nombre", label:"Nombre", editor:"textbox",sortable:true},
					{key:"descripcion", label:"Descripci&oacute;n", sortable:true, editor:"textbox",resizeable:true},
					//{key:"foto", label:"Foto", sortable:true, formatter:this.formatImg},
                                        {key:"tipo", label:"Tipo", editor:"textbox",sortable:true},
					{key:"estado", label:"Estado", sortable:true, editor:"textbox",resizeable:true},
					{key:"importancia", label:"Importancia", sortable:true},
                                        {key:"fecha", label:"Fecha", editor:"textbox",sortable:true},
                                        {key:"lugar", label:"Lugar", editor:"textbox",sortable:true}
				];
				this.myDataSource= new YAHOO.util.DataSource("../ManejadorTareas?action=consultarTodas");
				this.myDataSource.connMethodPost=true;
				this.myDataSource.responseType=YAHOO.util.DataSource.TYPE_XML;
				this.myDataSource.responseSchema = {
					resultNode: "tarea",
					fields: ["Lugar","nombre","descripcion","tipo","estado","importancia","fecha"]};
				var myConfigs = {
				  paginator: new YAHOO.widget.Paginator({
					  rowsPerPage: 5,
					  template: YAHOO.widget.Paginator.TEMPLATE_ROWS_PER_PAGE,
					  rowsPerPageOptions: [5,10,15],
					  pageLinks: 5
				  }),
				  draggableColumns:true
				}
				/*function onDbClick(oArgs){
					var elRow = oArgs.target;
					var oRecord = this.getRecord(elRow);
					var lat = oRecord.getData("lat");
					var lon = oRecord.getData("lon");

					function addtag(point, address) {
						var marker = new GMarker(point);
						GEvent.addListener(marker, "click", function() {
						marker.openInfoWindowHtml(address); } );
						return marker;
					}

					var point = new GLatLng(lat,lon);
					var address = '<b>'+name.value+'</b><br/><i>'+oRecord.getData("nombre")+'</i><br /><img src="'+oRecord.getData("foto")+'">JSLIBS</a>';
					var marker = addtag(point, address);
					map.addOverlay(marker);

				}*/

				this.myDataTable=new YAHOO.widget.DataTable("xml",myColumnDefs,this.myDataSource,myConfigs);
				//myDataTable.subscribe("rowDblclickEvent",onDbClick);
				myDataTable.subscribe("cellDblclickEvent",myDataTable.onEventShowCellEditor);

				this.onContextMenuClick = function(p_sType, p_aArgs, p_myDataTable) {
					var task = p_aArgs[1];
					if(task) {

						var elRow = this.contextEventTarget;
						elRow = p_myDataTable.getTrEl(elRow);

						if(elRow) {
							switch(task.index) {
								case 0:     // Eliminar fila despues de confirmar
									var oRecord = p_myDataTable.getRecord(elRow);
									if(confirm("Esta seguro de eliminar Nombre " +
											oRecord.getData("Nombre") + " (" +
											oRecord.getData("Descripcion") + ")?")) {
										p_myDataTable.deleteRow(elRow);
									}
							}
						}
					}
				};

				this.myContextMenu = new YAHOO.widget.ContextMenu("mycontextmenu",
						{trigger:this.myDataTable.getTbodyEl()});
				this.myContextMenu.addItem("Eliminar Registro");
				// Render the ContextMenu instance to the parent container of the DataTable
				this.myContextMenu.render("xml");
				this.myContextMenu.clickEvent.subscribe(this.onContextMenuClick, this.myDataTable);

				var onCellEdit = function(oArgs) {
					var elCell = oArgs.editor.getTdEl();
					var oOldData = oArgs.oldData;
					var oNewData = oArgs.newData;

				}
				myDataTable.subscribe("editorSaveEvent",onCellEdit);

			}
 function initialize(XML) {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(new GLatLng(-2.2022163588447077, -79.903564453125), 12);
    map.setUIToDefault();

    // Create a base icon for all of our markers that specifies the
    // shadow, icon dimensions, etc.
    var baseIcon = new GIcon(G_DEFAULT_ICON);
    baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
    baseIcon.iconSize = new GSize(20, 34);
    baseIcon.shadowSize = new GSize(37, 34);
    baseIcon.iconAnchor = new GPoint(9, 34);
    baseIcon.infoWindowAnchor = new GPoint(9, 2);

    // Creates a marker whose info window displays the letter corresponding
    // to the given index.

    function createMarker(point, index, descripcion, nombre, imagen) {
      // Create a lettered icon for this point using our icon class
      var letter = String.fromCharCode("A".charCodeAt(0) + index);
      var letteredIcon = new GIcon(baseIcon);
      letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";

      // Set up our GMarkerOptions object
      markerOptions = { icon:letteredIcon };
      var marker = new GMarker(point, markerOptions);

      GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml("<div id = 'mensaje' ><h3>"+nombre+"</h3><p>"+descripcion+"</p><img src='images/"+imagen+"'/></div>");
      });
      return marker;
    }
    // Add 10 markers to the map at random locations
    if(XML!=null){ 
        var place_mark = XML.getElementsByTagName("place_mark");
        for (var i = 0; i < place_mark.length; i++) {

            var descripcion = place_mark[i].getElementsByTagName("descripcion");
            var nombre = place_mark[i].getElementsByTagName("nombre");
            var imagen = place_mark[i].getElementsByTagName("imagen");
            var lat = place_mark[i].getElementsByTagName("latitud");
            var lng = place_mark[i].getElementsByTagName("longitud");
            var latlng = new GLatLng(lat[0].firstChild.nodeValue,lng[0].firstChild.nodeValue);
            map.addOverlay(createMarker(latlng, i, descripcion[0].firstChild.nodeValue,nombre[0].firstChild.nodeValue,imagen[0].firstChild.nodeValue));
        }
    }
  }
}